package StackDemo;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class PolandNotation {
    public static void main(String[] args) {
        String str = "1+((2+3)*4)-5";
        List<String> list = toInFixExpressionList(str);
        List<String> list1=parseSuffixExpreesionList(list);
        System.out.println(list1);
        int i =reaLize(list1);
        System.out.println(i);

     /*   String str = "4 5 + 8 * 6 + 8 2 / -";
        List<String> list = getListString(str);
       int i= reaLize(list);
        System.out.println(i);*/
    }
    //中缀转后缀表达式
    public static List<String> parseSuffixExpreesionList(List<String> list){
        Stack<String> s2 = new Stack<String>();
        List<String>  s1 = new ArrayList<String>();
        for(String s:list){
            if(s.matches("\\d+")){
                s1.add(s);
            }else if(s.equals("(")){
                s2.push(s);
            }else if(s.equals(")")){
                while(!s2.peek().equals("(")){
                    s1.add(s2.pop());}//将（出栈
                s2.pop();
            }
            else{
                while(s2.size()!=0&&Operation.getValue(s2.peek())>=Operation.getValue(s)){
                    s1.add(s2.pop());
                }
                s2.push(s);
            }
        }
        while(s2.size()!=0){
            s1.add(s2.pop());
        }
        return s1;
    }
    //把String类型转换成List数组
    public static List<String> toInFixExpressionList(String s){
        List<String> list = new ArrayList<String>();
        String str;
        int i =0;
        char c;
        do {
            if ((c=s.charAt(i))<48||(c=s.charAt(i))>57){
                list.add(""+c);
                i++;
            }else{
                str="";
                while(i<s.length()&&(c=s.charAt(i))>=48&&(c=s.charAt(i))<=57){
                    str+=c;
                    i++;
                }
                list.add(str);
            }
        }while(i<s.length());
        return list;
    }
    public static List getListString(String str) {
        List<String> list = new ArrayList<String>();
        String[] s = str.split(" ");
        for(String q:s){
            list.add(q);
        }
        return list;
    }
    //计算
    public static int reaLize(List<String> list){
        int num2;
        int num1;
        int req = 0;
        java.util.Stack<String> stack= new Stack<String>();
        for(String str:list){
            //如果是数字就入栈
            if(str.matches("\\d+")){
                    stack.push(str);
            }else{
                num2=Integer.parseInt(stack.pop());
                num1=Integer.parseInt(stack.pop());
                if(str.equals("+")){
                   req= num1+num2;
                }else if(str.equals("-")){
                    req= num1-num2;
                }else if(str.equals("*")){
                    req= num1*num2;
                }else if(str.equals("/")){
                    req= num1/num2;
                }else
                {
                    throw new RuntimeException("111");
                }
                stack.push(req+"");
            }
        }

        return req;
    }
}
//计算优先级
class Operation{
    private static int ADD=1;
    private static int SUB=1;
    private static int MUL=2;
    private static int DIV=2;
    public static int getValue(String str){
        int i = 0;
        switch(str) {
            case "+":
                i = ADD;
                break;
            case "-":
                i = SUB;
                break;
            case "*":
                i = MUL;
                break;
            case "/":
                i = DIV;
                break;
            default:
                System.out.println("Operation.getValue~~传值错误");
                break;
        }

        return i;
    }


}